%%%-------------------------------------------------------------------
%%% @author kk
%%% @copyright (C) 2024, <COMPANY>
%%% @doc
%%% 指定列表第几位之后的数据进行反转。如：指定[2,3,5,6,7,2]第3位后进行反转  2 3 5 2 7 6
%%% [2,3,5 | 6,7,2]
%%% @end
%%% Created : 19. 7月 2024 16:20
%%%-------------------------------------------------------------------
-module(d11).
-author("kk").

%% API
-export([reversePosition/2]).

%% 反转列表
reverse(List) -> reverse(List, 1, []).
reverse([], Index, Reverse_List) -> Reverse_List;
reverse([H | List], Index, Reverse_List) ->
  New_List = [H | Reverse_List],
  reverse(List, Index + 1, New_List).

%% 在指定位置开始反转
reversePosition(List,Position) -> reversePosition(List, 1, Position).
reversePosition([H | List], Index, Position) ->   % 1  2 3 4 [1,2];  2 3 4 [2,2];  3 4 [3,2];
  if
    Index =< Position -> [H | reversePosition(List, Index + 1, Position)]; % 不用反转
    true -> reverse([H | List]) % 反转  [3, 4]
  end.
